'''
Created on 2011-3-3

@author: jeffrey
'''
import unittest
from models import *
import service

class TestService(unittest.TestCase):
    def setUp(self):       
        # create an user model
        user = User(name='Test')
        
        # add this user model into datastore and check
        service.saveUser(user)  
    
    #1. User Service------------------------------------------------------------------
    def test_saveUser(self):
        # create an user model
        #user = User(name='Test')
        
        # add this user model into datastore and check
        #service.saveUser(user)        
        other = service.getUserList({'name': 'Test'}).get()
        self.assertEqual('Test', other.name)
        
        # update the user model and check
        other.name='ABC'
        service.saveUser(other)
        self.assertEquals(None, service.getUserList({'name': 'Test'}))        
        self.assertEquals(1, service.getUserList({'name': 'ABC'}).count())
    
    def test_deleteUser(self):
        self.test_saveUser()
        user = service.getUserList({'name': 'ABC'}).get()
        service.deleteUser(user)
        self.assertEquals(None, service.getUserList())
    
    def test_addUserCredit(self):
        self.test_saveUser()
        user = service.getUserList({'name': 'ABC'}).get()
        self.assertEqual(0, user.credit)
        service.addUserCredit(user, 100)
        
        user = service.getUserList({'name': 'ABC'}).get()
        self.assertEqual(100, user.credit)
    
    def test_getUserGrade(self):
        self.test_saveUser()
        user = service.getUserList({'name': 'ABC'}).get()
        self.test_saveGrade()
        
        self.assertEqual('Level1', service.getUserGrade(user).name)
        
        service.addUserCredit(user, 99)
        self.assertEqual('Level1', service.getUserGrade(user).name)
        
        service.addUserCredit(user, 100)
        self.assertEqual('Level2', service.getUserGrade(user).name)
        
    #2. SNS Service------------------------------------------------------------------   
    def test_saveSNS(self):
        self.assertEqual(None, service.getSNSList())
        
        sns = SNS(name='douban', url='http://www.douban.com')
        service.saveSNS(sns)
        self.assertEqual(1, service.getSNSList().count())
        self.assertEqual(1, service.getSNSList({'name':'douban', 'url': 'http://www.douban.com'}).count())
        self.assertEqual(None, service.getSNSList({'name': 'sina'}))
        
        sns.name='sina'
        sns.url = 'http://t.sina.com.cn'
        service.saveSNS(sns)
        self.assertEqual(None, service.getSNSList({'name': 'douban'}))
        self.assertEqual(1, service.getSNSList().count())
        self.assertEqual(1, service.getSNSList({'name':'sina', 'url': 'http://t.sina.com.cn'}).count())
    
    def test_deleteSNS(self):
        self.test_saveSNS()
        sns = service.getSNSList({'name': 'sina'}).get()
        service.deleteSNS(sns)
        self.assertEquals(None, service.getSNSList())

    #3. UserSNS Service------------------------------------------------------------------    
    def test_saveUserSNS(self):
        self.assertEqual(None, service.getUserSNSList())
        
        self.test_saveUser()
        self.test_saveSNS()
        user = service.getUserList().get()
        sns = service.getSNSList().get()
        
        userSNS = UserSNS(user=user, sns=sns)
        service.saveUserSNS(userSNS)        
        self.assertEqual(1, service.getUserSNSList().count())
    
    def test_deleteUserSNSByUser(self):
        self.test_saveUserSNS()
        
        user = service.getUserList().get()
        service.deleteUser(user)
        self.assertEqual(None, service.getUserSNSList())
    
    def test_deleteUserSNSBySNS(self):
        self.test_saveUserSNS()
        
        sns = service.getSNSList().get()
        service.deleteSNS(sns)
        self.assertEqual(None, service.getUserSNSList())

    #4. Message Service------------------------------------------------------------------
    def test_saveMessage(self):
        self.assertEquals(None, service.getMessageList())
        
        #self.test_saveUser()
        user = service.getUserList().get()
        
        message = Message(title='Test', content='Test', receiveuser=user)
        service.saveMessage(message)
        self.assertEquals(1, service.getMessageList().count())
        self.assertEquals(1, service.getMessageList({'title': 'Test', 'content': 'Test'}).count())
        
        message.title = 'ABC'
        message.content = 'ABC'
        service.saveMessage(message)
        self.assertEquals(1, service.getMessageList().count())
        self.assertEquals(None, service.getMessageList({'name': 'Test', 'content': 'Test'}))
        self.assertEquals(1, service.getMessageList({'title': 'ABC', 'content': 'ABC'}).count())
    
    def test_deleteMessageByUser(self):
        self.test_saveMessage()
        
        user = service.getUserList().get()
        service.deleteUser(user)
        self.assertEquals(None, service.getMessageList())
        self.assertEquals(None, service.getMessageList({'receiveuser': user}))
    
    def test_deleteMessage(self):
        self.test_saveMessage()
        
        self.assertEquals(1, service.getMessageList().count())
        message = service.getMessageList().get()
        service.deleteMessage(message)
        self.assertEquals(None, service.getMessageList())
        self.assertEquals(None, service.getMessageList({'title': 'ABC'}))
        
    #5. Comment Service------------------------------------------------------------------
    def test_saveComment(self):
        self.assertEquals(None, service.getCommentList())
        
        self.test_saveMessage()
        user = service.getUserList().get()
        message = service.getMessageList().get()
        
        comment = Comment(message=message, user=user, content='Test')
        service.saveComment(comment)
        self.assertEquals(1, service.getCommentList().count())
        
        comment.content='ABC'
        service.saveComment(comment)
        self.assertEquals(1, service.getCommentList().count())
        self.assertEquals(None, service.getCommentList({'content': 'Test'}))
        self.assertEquals(1, service.getCommentList({'content': 'ABC'}).count())
    
    def test_deleteCommentByUser(self):
        self.test_saveComment()
        
        self.assertEquals(1, service.getCommentList().count())
        user = service.getUserList().get()
        service.deleteUser(user)
        self.assertEquals(None, service.getCommentList())
    
    def test_deleteCommentByMessage(self):
        self.test_saveComment()
        
        self.assertEquals(1, service.getCommentList().count())
        message = service.getMessageList().get()
        service.deleteMessage(message)
        self.assertEquals(None, service.getCommentList())
    
    #6. Tag Service------------------------------------------------------------------
    def test_saveTag(self):
        self.assertEqual(None, service.getTagList())
        
        #self.test_saveUser()
        user = service.getUserList().get()
        tag = Tag(name='Test', user=user)
        service.saveTag(tag)
        
        self.assertEqual(1, service.getTagList().count())
        self.assertEqual(1, service.getTagList({'name': 'Test', 'user': user}).count())
    
    def test_deleteTag(self):
        self.test_saveTag()
        
        self.assertEqual(1, service.getTagList().count())
        tag = service.getTagList().get()
        service.deleteTag(tag)
        self.assertEqual(None, service.getTagList())
    
    def test_deleteTagByUser(self):
        self.test_saveTag()
        self.assertEqual(1, service.getTagList().count())
        user = service.getUserList().get()
        service.deleteUser(user)
        self.assertEqual(None, service.getTagList())

    #7. MessageTag Service------------------------------------------------------------
    def test_saveMessageTag(self):
        self.assertEqual(None, service.getMessageTagList())
        
        self.test_saveMessage()
        self.test_saveTag()
        message = service.getMessageList().get()
        tag = service.getTagList().get()
        messageTag = MessageTag(message=message, tag=tag)
        service.saveMessageTag(messageTag)
        self.assertEqual(1, service.getMessageTagList().count())
        self.assertEqual(1, service.getMessageTagList({'message': message, 'tag': tag}).count())
        
    def test_deleteMessageTagByMessage(self):
        self.test_saveMessageTag()
        self.assertEqual(1, service.getMessageTagList().count())
        message = service.getMessageList().get()
        service.deleteMessage(message)
        self.assertEqual(None, service.getMessageTagList())
    
    def test_deleteMessageTagByTag(self):
        self.test_saveMessageTag()
        self.assertEqual(1, service.getMessageTagList().count())
        tag = service.getTagList().get()
        service.deleteTag(tag)
        self.assertEqual(None, service.getMessageTagList())

    #8. User Service------------------------------------------------------------------
    def test_saveGrade(self):
        self.assertEqual(None, service.getGradeList())
        grade = Grade(name='Level1', credit=0)
        service.saveGrade(grade)
        self.assertEqual(1, service.getGradeList({'name': 'Level1', 'credit': 0}).count())
        
        grade = Grade(name='Level2', credit=100)
        service.saveGrade(grade)
        self.assertEqual(1, service.getGradeList({'name': 'Level2', 'credit': 100}).count())
    
    def test_deleteGrade(self):
        self.test_saveGrade()
        self.assertEqual(2, service.getGradeList().count())
        
        grade = service.getGradeList().get()
        service.deleteGrade(grade)
        self.assertEqual(1, service.getGradeList().count())
        grade = service.getGradeList().get()
        service.deleteGrade(grade)
        self.assertEqual(None, service.getGradeList())

    #9. UserStatistic Service------------------------------------------------------------------
    def test_saveUserStatistic(self):
        self.assertEqual(None, service.getUserStatisticList())
        
        user = service.getUserList().get()
        userStatistic = UserStatistic(user=user)
        service.saveUserStatistic(userStatistic)
        self.assertEqual(1, service.getUserStatisticList().count())
        self.assertEqual(1, service.getUserStatisticList({'user': user}).count())
    
    def test_deleteUserStatistic(self):
        self.test_saveUserStatistic()
        userStatistic = service.getUserStatisticList().get()
        service.deleteUserStatistic(userStatistic)
        self.assertEqual(None, service.getUserStatisticList())
    
    def test_deleteUserStatisticByUser(self):
        self.test_saveUserStatistic()
        user = service.getUserList().get()
        service.deleteUser(user)
        self.assertEqual(None, service.getUserStatisticList())
    
    #10. SystemParameter Service------------------------------------------------------------------
    def test_saveSystemParameter(self):
        self.assertEqual(None, service.getSystemParameterList())
        
        systemParameter = SystemParameter(pkey='TestKey', pvalue='TestValue')
        service.saveSystemParameter(systemParameter)
        self.assertEqual(1, service.getSystemParameterList().count())
        self.assertEqual(1, service.getSystemParameterList({'pkey': 'TestKey', 'pvalue': 'TestValue'}).count())
    
    def test_deleteSystemParameter(self):
        self.test_saveSystemParameter()
        systemParameter = service.getSystemParameterList({'pkey': 'TestKey', 'pvalue': 'TestValue'}).get()
        service.deleteSystemParameter(systemParameter)
        self.assertEqual(None, service.getSystemParameterList())       
    
    def test_getParaValue(self):
        self.test_saveSystemParameter()
        
        self.assertEqual(None, service.getParaValue('abc'))
        self.assertEqual(None, service.getParaValue('abc', None))
        self.assertEqual('TestValue', service.getParaValue('TestKey'))
        self.assertEqual('TestValue', service.getParaValue('TestKey', 'abc'))
        self.assertEqual('abc', service.getParaValue('abc', 'abc'))
    
    #11. MessageType Service------------------------------------------------------------------
    def test_saveMessageType(self):
        self.assertEqual(None, service.getMessageTypeList())
        
        messageType = MessageType(name='test')
        service.saveMessageType(messageType)
        self.assertEqual(1, service.getMessageTypeList().count())
        self.assertEqual(1, service.getMessageTypeList({'name': 'test'}).count())
    
    def test_deleteMessageType(self):
        self.test_saveMessageType()
        messageType = service.getMessageTypeList({'name': 'test'}).get()
        
        self.test_saveMessage()
        message = service.getMessageList().get()
        message.messagetype = messageType
        service.saveMessage(message)
        
        self.assertEqual('test', service.getMessageList().get().messagetype.name)
        
        self.assertEqual(1, service.getMessageList({'messagetype': messageType}).count())
        
        service.deleteMessageType(messageType)
        self.assertEqual(None, service.getMessageTypeList())
        
        self.assertEqual(None, service.getMessageList().get().messagetype)
        
    
if __name__ == '__main__':
    unittest.main()